package com.paradogs.common.misc;

import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.baomidou.mybatisplus.extension.service.IService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.paradogs.common.utils.Virtual;
import org.apache.ibatis.session.SqlSession;

import java.io.Serializable;
import java.util.Collection;
import java.util.Map;
import java.util.Optional;
import java.util.function.BiConsumer;
import java.util.function.Consumer;
import java.util.function.Function;

/**
 * @author: yumi
 * @date: 2025/3/29  15:36
 * @Description: 使 mybatis-plus db 操作支持虚拟线程挂起
 */
public class VirtualSupportServiceImpl<M extends BaseMapper<T>, T> extends ServiceImpl<M, T> implements IService<T> {

    @Override
    public boolean saveBatch(Collection<T> entityList, int batchSize) {
        return Virtual.of(() -> super.saveBatch(entityList, batchSize));
    }

    @Override
    public boolean saveOrUpdate(T entity) {
        return Virtual.of(() -> super.saveOrUpdate(entity));
    }

    @Override
    public boolean saveOrUpdateBatch(Collection<T> entityList, int batchSize) {
        return Virtual.of(() -> super.saveOrUpdateBatch(entityList, batchSize));
    }

    @Override
    public boolean updateBatchById(Collection<T> entityList, int batchSize) {
        return Virtual.of(() -> super.updateBatchById(entityList, batchSize));
    }

    @Override
    public boolean removeById(Serializable id) {
        return Virtual.of(() -> super.removeById(id));
    }

    @Override
    public boolean removeByIds(Collection<?> list) {
        return Virtual.of(() -> super.removeByIds(list));
    }

    @Override
    public boolean removeById(Serializable id, boolean useFill) {
        return Virtual.of(() -> super.removeById(id, useFill));
    }

    @Override
    public boolean removeBatchByIds(Collection<?> list, int batchSize) {
        return Virtual.of(() -> super.removeBatchByIds(list, batchSize));
    }

    @Override
    public boolean removeBatchByIds(Collection<?> list, int batchSize, boolean useFill) {
        return Virtual.of(() -> super.removeBatchByIds(list, batchSize, useFill));
    }

    @Override
    public T getOne(Wrapper<T> queryWrapper, boolean throwEx) {
        return Virtual.of(() -> super.getOne(queryWrapper, throwEx));
    }

    @Override
    public Optional<T> getOneOpt(Wrapper<T> queryWrapper, boolean throwEx) {
        return Virtual.of(() -> super.getOneOpt(queryWrapper, throwEx));
    }

    @Override
    public Map<String, Object> getMap(Wrapper<T> queryWrapper) {
        return Virtual.of(() -> super.getMap(queryWrapper));
    }

    @Override
    public <V> V getObj(Wrapper<T> queryWrapper, Function<? super Object, V> mapper) {
        return Virtual.of(() -> super.getObj(queryWrapper, mapper));
    }

    // 注意：以下两个executeBatch方法需要特殊处理返回值
    @Override
    protected boolean executeBatch(Consumer<SqlSession> consumer) {
        return Virtual.of(() -> super.executeBatch(consumer));
    }

    @Override
    protected <E> boolean executeBatch(Collection<E> list, int batchSize, BiConsumer<SqlSession, E> consumer) {
        return Virtual.of(() -> super.executeBatch(list, batchSize, consumer));
    }

    @Override
    protected <E> boolean executeBatch(Collection<E> list, BiConsumer<SqlSession, E> consumer) {
        return Virtual.of(() -> super.executeBatch(list, consumer));
    }
}
